package com.leo.foodmaster.admin.customer.controller;

import com.leo.foodmaster.commons.entity.Result;
import org.springframework.web.bind.annotation.*;
import java.util.Arrays;
import java.util.List;
import cn.hutool.core.bean.BeanUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import springfox.documentation.annotations.ApiIgnore;
import org.springframework.web.bind.annotation.PostMapping;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.leo.foodmaster.admin.customer.dto.CustomerDeliveryRouterDTO;
import com.leo.foodmaster.admin.customer.entity.CustomerDeliveryRouterEntity;
import com.leo.foodmaster.admin.customer.service.ICustomerDeliveryRouterService;
import com.leo.foodmaster.admin.customer.mapper.CustomerDeliveryRouterMapper;
import javax.annotation.Resource;

/**
 * 配送线路
 *
 * @author Mark sunlightcs@gmail.com
 * @since 1.0.0 2021-11-20
 */
@RestController
@RequestMapping("/api/customer/deliveryRouter")
@Api(tags = "配送线路")
@Slf4j
public class CustomerDeliveryRouterController {
    @Resource
    private ICustomerDeliveryRouterService icustomerDeliveryRouterService;
    @Resource
    private CustomerDeliveryRouterMapper customerDeliveryRouterMapper ;


    @GetMapping("/page")
    @ApiOperation("分页")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "current", value = "当前页码，从1开始", paramType = "query", required = true, dataType="int") ,
        @ApiImplicitParam(name = "size", value = "每页显示记录数", paramType = "query",required = true, dataType="int")
    })
    public Result<IPage<CustomerDeliveryRouterDTO>> page(@ApiIgnore Page<CustomerDeliveryRouterDTO> page, @ApiIgnore CustomerDeliveryRouterDTO customerDeliveryRouterDTO){
    	IPage<CustomerDeliveryRouterDTO> pageList = icustomerDeliveryRouterService.pageList(page,customerDeliveryRouterDTO);
        return Result.success(pageList);
    }


    @GetMapping
    @ApiOperation("信息")
    public Result<CustomerDeliveryRouterDTO> get(@RequestParam Long id,@ApiIgnore CustomerDeliveryRouterDTO customerDeliveryRouterDTO){
    	CustomerDeliveryRouterEntity byId = icustomerDeliveryRouterService.getById(id);
    	if (BeanUtil.isEmpty(byId)) {
    		return Result.success();
		}
    	BeanUtil.copyProperties(byId, customerDeliveryRouterDTO);
    	return Result.success(customerDeliveryRouterDTO);
    }

    @PostMapping
    @ApiOperation("保存")
    public Result<Boolean> save(@ApiIgnore @RequestBody CustomerDeliveryRouterDTO dto){
        CustomerDeliveryRouterEntity entity = new CustomerDeliveryRouterEntity();
        BeanUtil.copyProperties(dto,entity);
        Boolean save = icustomerDeliveryRouterService.save(entity);
        return  Result.success(save);
    }


    @PutMapping
    @ApiOperation("修改")
    public Result<Boolean> update(@ApiIgnore @RequestBody CustomerDeliveryRouterDTO dto){
        CustomerDeliveryRouterEntity entity = new CustomerDeliveryRouterEntity();
        BeanUtil.copyProperties(dto,entity);
        boolean updateById = icustomerDeliveryRouterService.updateById(entity);
        return  Result.success(updateById);
    }


    @DeleteMapping
    @ApiOperation("删除")
    public Result<Integer> delete(@RequestBody Integer[] ids){
    	List<Integer> asList = Arrays.asList(ids);
    	int deleteBatchIds = customerDeliveryRouterMapper.deleteBatchIds(asList);
    	return Result.success(deleteBatchIds);
    }

}
